#include "companydialog.h"
#include "ui_companydialog.h"
#include "model/data.h"

CompanyDialog::CompanyDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::CompanyDialog)
{
    ui->setupUi(this);
    init();
}

CompanyDialog::~CompanyDialog()
{
    delete ui;
}

#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
void CompanyDialog::set(const model::CompanyInfo &companyInfo)
{
    ui->company_card->set(companyInfo);

    auto setLayout = [](QWidget* container) {
        QHBoxLayout *layout = new QHBoxLayout(container);
        layout->setContentsMargins(0, 0, 0, 0);
        layout->setSpacing(20);
        return layout;
    };

    auto parent = _container;
    auto getTag = [parent, setLayout](const QString& tag, int pct){
        QWidget *container = new QWidget(parent);
        container->setStyleSheet("background-color: transparent;");
        container->setFixedHeight(60);
        container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        auto layout = setLayout(container);

        auto content = new QPushButton(tag);
        content->setStyleSheet("background-color: transparent;  text-align: left; padding-left: 60px;");
        content->setFixedSize(280, 60);
        layout->addWidget(content);

        auto percent = new QPushButton(QString::number(pct) + " %");
        percent->setFixedSize(50, 50);
        percent->setStyleSheet("background-color: transparent;");
        layout->addWidget(percent);

        auto btn = new QPushButton;
        btn->setFixedSize(50, 50);
        btn->setStyleSheet("background-color: transparent;");
        btn->setIcon(QIcon(":/ui/btn_close_100.png"));
        layout->addWidget(btn);
        return container;
    };

    auto container = _container;
    auto setTags = [container, getTag](const QList<QString>& tags, const QList<qint64>& pcts) {
        for(int i = 0, size = std::min(tags.size(), pcts.size()); i < size; ++i) {
            auto curTag = getTag(tags[i], pcts[i]);
            container->layout()->addWidget(curTag);
        }
    };

    auto content = new QPushButton("最常考的知识点：");
    content->setStyleSheet("background-color: transparent;  text-align: left; padding-left: 20px;");
    content->setFixedSize(280, 60);
    _container->layout()->addWidget(content);

    setTags(companyInfo.topTags, companyInfo.topTagsPct);

    content = new QPushButton("最近上升的知识点：");
    content->setStyleSheet("background-color: transparent;  text-align: left; padding-left: 20px;");
    content->setFixedSize(280, 60);
    _container->layout()->addWidget(content);

    setTags(companyInfo.upTags, companyInfo.upTagsPct);
}

void CompanyDialog::init()
{
    initPanel();
    initTopK();
    initStyleSheet();
    initBody();
}

#include <QGraphicsDropShadowEffect>
void CompanyDialog::initPanel()
{
    ui->panel->setFixedSize(1500, 1000);

    // 顶层(MainWidget)：无边框 + 透明，仅做容器
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 给内容面板(panel)加阴影
    auto *effect = new QGraphicsDropShadowEffect(ui->panel);
    effect->setColor(Qt::black);
    effect->setOffset(0, 0); // 0偏移
    effect->setBlurRadius(6); // 模糊半径
    ui->panel->setGraphicsEffect(effect);

    ui->panel->layout()->setSpacing(0);
    ui->panel->layout()->setContentsMargins(0, 10, 0, 10);

    ui->card->setFixedWidth(500);
    ui->card->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);

    ui->info->setFixedHeight(250);
    ui->info->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->topK->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    ui->content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    ui->content_head->layout()->setSpacing(30);
    ui->content_head->layout()->setContentsMargins (20, 0, 20, 0);
    ui->content_head->setFixedHeight(80);
    ui->content_head->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    ui->btn_interview->setFixedSize(90, 60);
    ui->btn_question->setFixedSize(90, 60);
    ui->btn_text->setFixedSize(90, 60);

    ui->btn_close->setFixedSize(45, 45);
}

#include "pagecontroller.h"
#include "interviewlistwidget.h"
#include "questionlistwidget.h"
void CompanyDialog::initBody()
{
    auto controller = new PageController(ui->pages, this);

    QWidget* curPage = new InterviewListWidget(this);
    auto id = controller->addPage(ui->btn_interview, curPage);
    ui->pages->addWidget(curPage);

    curPage = new QuestionListWidget(this);
    controller->addPage(ui->btn_question, curPage);
    ui->pages->addWidget(curPage);

    curPage = new QuestionListWidget(this);
    controller->addPage(ui->btn_text, curPage);
    ui->pages->addWidget(curPage);

    controller->switchToPage(id);
}

void CompanyDialog::initStyleSheet()
{
    this->setStyleSheet("background-color: transparent;");
    ui->panel->setStyleSheet(R"(background-color: rgb(247, 249, 252); border-radius: 12px; color: #333333;)");
    ui->content_head->setStyleSheet(R"(
    #content_head {
        background-color: transparent;
    })");
    ui->btn_close->setStyleSheet(R"(
    QPushButton {
        background-repeat: no-repeat;
        background-position: center;
        background-image: url(:/ui/btn_close_50.png);
        border: none;
        background-color: transparent;
    }

    QPushButton:hover,
    QPushButton:pressed {
        background-image: url(:/ui/btn_close_100.png);
    })");

    ui->btn_interview->setStyles(R"(border-radius: 12px; )", R"(background-color: transparent; color: #787F8D;)", R"(background-color: #FC3D4B; color: #ffffff;)");
    ui->btn_question->setStyles(R"(border-radius: 12px; )", R"(background-color: transparent; color: #787F8D;)", R"(background-color: #FC3D4B; color: #ffffff;)");
    ui->btn_text->setStyles(R"(border-radius: 12px; )", R"(background-color: transparent; color: #787F8D;)", R"(background-color: #FC3D4B; color: #ffffff;)");
}

void CompanyDialog::initTopK()
{
    // 1) 新建一个主布局，设置给 ui->topK
    QVBoxLayout *mainLayout = new QVBoxLayout(ui->topK);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 2) 新建 QScrollArea 及其内部容器
    _scrollArea = new QScrollArea(ui->topK);

    _container = new QWidget;
    _container->setStyleSheet(R"( background-color: transparent;)");

    QVBoxLayout* containerLayout = new QVBoxLayout(_container);
    containerLayout->setSpacing(10);
    containerLayout->setContentsMargins(20, 0, 20, 0);
    containerLayout->setAlignment(Qt::AlignTop); // 布局内所有子控件顶对齐

    // 3) 设置 scrollArea
    _scrollArea->setWidget(_container);
    _scrollArea->setWidgetResizable(true);
    _scrollArea->setStyleSheet(R"( QScrollArea { border: none; background-color: transparent; } QScrollArea > QWidget { background-color: transparent; } QScrollBar:vertical { background-color: transparent; /* 透明背景 */ width: 9px; border: none; } QScrollBar::handle:vertical { background-color: #dcdfe4; border-radius: 4px; min-height: 20px; /* 最小高度 */ } QScrollBar::handle:vertical:hover { background-color: #bec3ca; } QScrollBar::handle:vertical:pressed { background-color: #a8adb5; /* 新增:按下状态 */ } /* 关键:定义滑块上下的空白区域 */ QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: transparent; } QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0px; /* 隐藏箭头 */ background: transparent; } QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical { background: none; } )");
    _scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);

    // 4) 将 scrollArea 加到主布局里
    mainLayout->addWidget(_scrollArea);
}

#include <QMouseEvent>
void CompanyDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        _dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        _isDraging = true;
        event->accept();
    }
}

void CompanyDialog::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && _isDraging) {
        move(event->globalPosition().toPoint() - _dragPosition);
        event->accept();
    }
}

void CompanyDialog::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    _isDraging = false;
}

void CompanyDialog::on_btn_close_clicked()
{
    reject();
}

